#ifndef _TWIEEPROM_H
#define _TWIEEPROM_H   1
/************************************************************************* 
* Title:    C include file for the I2C master interface 
*           (i2cmaster.S or twimaster.c)
* Author:   Brent Reamer aka BrentBXR (BrentBXR@Gmail.com)
* File:     $Id: twi_eeprom
* Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
* Target:   any AVR device
* Include:	Include Pete Fluarys TWI Master
* Usage:    see Doxygen manual
**************************************************************************/

#ifdef DOXYGEN
/**
 @defgroup breamer twi.library
 @code #include "twi_eeprom.h" @endcode
  
 @brief An addon to Pete Fleurys TWIMaster.c Library

 LONGDESC

 @note 
    Anyone familure  with the TWI eeproms  such as  the Microchips  (what this was developed with)
	then you know that you can read/write by opening the communication with the bus in the correct
	mode and just  keep spitting bytes into the bus.  This library just reads/writes one byte at a
	time; this is  because its simpler then spitting/taking  bytes from arrays or  streams. Mainly
	for the newer users. This way  still works fine; its just slower then it could be. If speed is
	needed then you will have to use the TWI  master.c by itself;  which is also very easy, infact
	in Pete's TWImaster.h file it has information/examples in it for driving eeproms with just the
	twimaster library.
    
 @author Brent C. Reamer aka BrentBXR@AVRFreaks Forum

 @par Device Addressing

 @code
	Device Addressing:

	[A][D][D][R][#][#][#][X}

	A/D/D/R = Device Family ID; This specific EEPROM type is always [1][0][1][0]

	# 		= The specific device ID; Current set to all logic high or [1][1][1]

	X		= Read or Write mode. [1]=Read mode [0]=Write mode

	EG: To Address on @READ mode:
								   [1][0][1][0][1][1][1][1]

		To Address on @WRITE mode:
								   [1][0][1][0][1][1][1][0]
								   
	@@ But when using this library all you need to know is the first 7 bits and send
	   that to the functions. You do not need to send the RW bit as the functions set
	   that themselfs.
	   
 @endcode

*/
#endif /* DOXYGEN */

/**@{*/

#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
#endif

#include <avr/io.h>
#include "twimaster.h"

#ifndef _I2CMASTER_H
#error "This library requires Pete Fleurys TWI Master Library !"
#endif


// Library Settings
#define TWIE_USE_WR_PROTECT 0 //Set this to 1 if you want to use a spare pin to control write protection
							  //If this is enabled them it also enables a few more functions/commands
							  //that enable or disable the write protection, they are:
							  // - eeprom_disablewrite();
							  // - eeprom_enablewrite();
							  // - eeprom_isdisabled(); (returns 1 if disabled, 0 if not);
							  //These functions are not described below as this feature is naturally disabled.
							  //as long as you have control of your code and hardware; you shouldnt need this.
							  //but some projects may call for it, so i added it real fast (all by defines).
							  //-> Also note that the code in .c does not quickly enable before trying to write
							  //so you must enable it before trying to write if you have this option on.

#if (TWIE_USE_WR_PROTECT)
#define TWIE_WRPROTECT_PD PD5
#define TWIE_WRPROTECT_PIN PIND
#define TWIE_WRPROTECT_DDR DDRD
#define TWIE_WRPROTECT_PORT	PORTD

#define TWIE_WRPROTECT_DISABLEWRITE() (TWIE_WRPROTECT_DDR |= (1<<TWIE_WRPROTECT_PD)); (TWIE_WRPROTECT_PORT |= (1<<TWIE_WRPROTECT_PD))
#define TWIE_WRPROTECT_ENABLEWRITE() (TWIE_WRPROTECT_DDR &= ~(1<<TWIE_WRPROTECT_PD)); (TWIE_WRPROTECT_PORT &= ~(1<<TWIE_WRPROTECT_PD)) 
#define TWIE_WRPROTECT_ISDISABLED() (TWIE_WRPROTECT_PIN & (1<<TWIE_WRPROTECT_PD))

#define eeprom_enablewrite() TWIE_WRPROTECT_ENABLEWRITE();
#define eeprom_disablewrite() TWIE_WRPROTECT_DISABLEWRITE();
#define eeprom_isdisabled() TWIE_WRPROTECT_ISDISABLED();
#endif


/**
 @brief initilizes Pete Flaury's TWIMaster library, thus enabling this library

 @retval @null
 */
#define eeprom_init() i2c_init()

 
 
/**
 @brief Writes one (1) byte to the specified device @ the specified address

 @param   uint8_t	wDeviceAddr: Device address
 @param   uint8_t	wByte: The byte to write to the device
 @param   uint16_t	wAddr: Location address to store byte in EEPROM memory
 @retval  int		:1 = Write OK / 0 = Write FAIL
 */
extern unsigned int eeprom_wr_byte(uint8_t wDeviceAddr, uint8_t wByte, uint16_t wAddr);

/**
 @brief Writes a string to the specified device @ the specified address

 @param   uint8_t	wDeviceAddr: Device address
 @param   uint8_t	*s: String to write
 @param   uint16_t	wAddr: Location address to store byte in EEPROM memory
 @retval  int		:1 = Write OK / 0 = Write FAIL
 */
extern unsigned int eeprom_wr_string(uint8_t wDeviceAddr, const char *s, uint16_t wAddr);

/**
 @brief Reads one (1) byte from specified device @ specified memory address

 @param   uint8_t	wDeviceAddr: Device address
 @param   uint16_t	wAddr: Location address to store byte in EEPROM memory
 @retval  int		: the byte
 */
extern unsigned int eeprom_rd_byte(uint8_t wDeviceAddr, uint16_t wAddr);
/**@}*/
#endif
